import config from '@/config'
import storage from '@/utils/storage'
import constant from '@/utils/constant'
import {
	login,
	logout,
	getInfo
} from '@/api/login'
import {
	getInstitutionsSetting
} from '@/api/platform/institutions.js'
import {
	getToken,
	setToken,
	removeToken
} from '@/utils/auth'

const baseUrl = config.baseUrl

const user = {
	state: {
		token: getToken(),
		// name: storage.get(constant.name),
		userInfo:storage.get(constant.userInfo),//用户信息
		logInType:storage.get(constant.logInType),//用户身份 1医生  2个人  普通
		institution:storage.get(constant.institution),//机构信息
        permissionList:storage.get(constant.permissionList),//权限控制
		detailObj: {}, //传递到下一个页面对象值
		consultaServiceObj: {}, //普通会诊申请  特需会诊申请
		loginForm: {}, //会诊申请后没登录下返回创建的账号密码
		hospitalCode:{},//医院二维码传递过来的值 hd:张贴医院id pd:所属科室id area:张贴病区
		delivery:'',  // 医院二维码传递过来的值 1:现场送检
		
	},

	mutations: {
		SET_TOKEN: (state, token) => {
			state.token = token
			setToken(token)
		},
		// SET_NAME: (state, name) => {
		// 	state.name = name
		// 	storage.set(constant.name, name)
		// },
		setUserInfo: (state, item)=>{
			state.userInfo = item
			storage.set(constant.userInfo, item)
		},
		setLogInType: (state, item)=>{
			state.logInType = item
			storage.set(constant.logInType, item)
		},
		setInstitution: (state, item)=>{
			state.institution = item
			storage.set(constant.institution, item)
		},
		setPermissionList: (state, item) => {
			state.permissionList = item
		    storage.set(constant.permissionList, item)
		},
		setDetailObj: (state, item) => {
			state.detailObj = item
		},
		setConsultaServiceObj: (state, item) => {
			state.consultaServiceObj = item
		},
		setLoginForm: (state, item) => {
			state.loginForm = item
		},
		setHospitalCode: (state, item) => {
			state.hospitalCode = item
		},
		setDelivery: (state, item) => {
			state.delivery = item
		},
		
		

	},

	actions: {
		// 登录
		Login({
			commit
		}, userInfo) {
			console.log('userInfo---',userInfo)
			const username = userInfo.username
			const password = userInfo.password
			return new Promise((resolve, reject) => {
				login(username, password).then(res => {
					commit('SET_TOKEN', res.data.authorization)
					let roleName = ''
					res.data.userInfo.roles.forEach((item, index) => {
						if (index + 1 < res.data.userInfo.roles.length) {
							roleName = roleName + item.name + ','
						} else {
							roleName = roleName + item.name
						}
					
					})
					const user = {
						id: res.data.userInfo.id,
						name: res.data.userInfo.name,
						headImgUrl: res.data.userInfo.headUrl,
						phone: res.data.userInfo.phone,
						roleName: roleName
					}
					commit('setUserInfo', user)
					// 判断是否是医生,还是个人
					let logInType = 1 // 医生
					let dataroles = res.data['ui-item']
					let data = []
					if (dataroles && dataroles.length == 1) {
						if (dataroles[0].permissionList[0].code == 'user-workflow') { // 普通用户登录远程
							logInType = 2 //个人
						}
					}
					const permissionList = data.concat(dataroles)
					console.log(permissionList)
					commit('setPermissionList',permissionList)
					commit('setLogInType',logInType)
					// 如果是医生的话,获取会诊设置
					if (res.data.userInfo.institutionId) {
						getInstitutionsSetting(res.data.userInfo.institutionId).then(res => {
							if (res.code == 200) {
								const info = res.data
								const institution = {
									id: info.id,
									name: info.name,
									canConsultation: info.canConsultation,
									canFinalVerify: info.canFinalVerify,
									finalExertId: info.finalExertId
								}
								commit('setInstitution',institution)
							}
						})
					}

					resolve()
				}).catch(error => {
					reject(error)
				})
			})
		},

		// 获取用户信息
		GetInfo({
			commit,
			state
		}) {
			return new Promise((resolve, reject) => {
				getInfo().then(res => {
					// const user = res.user
					// const avatar = (user == null || user.avatar == "" || user.avatar == null) ?
					// 	require("@/static/images/profile.jpg") : baseUrl + user.avatar
					
					// commit('SET_NAME', username)
					// uni.setStorageSync('isPatient', isPatient)
					resolve(res)
				}).catch(error => {
					reject(error)
				})
			})
		},

		// 退出系统
		LogOut({
			commit,
			state
		}) {
			return new Promise((resolve, reject) => {
				logout(state.token).then(() => {
					commit('SET_TOKEN', '')
					commit('setLogInType','')
					storage.clean()
					removeToken()
					resolve()
				}).catch(error => {
					commit('SET_TOKEN', '')
					commit('setLogInType','')
					storage.clean()
					removeToken()
					resolve()
					// reject(error)
				})
			})
		}
	}
}

export default user